home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / histo_simulate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-20  |  5.8 KB  |  200 lines

  1. /*
  2. % histo_simulate.c - build image frames according to histogram which is
  3. %        produced by mhisto.
  4. %
  5. *********************************************************************
  6. *********               COPYRIGHT NOTICE                *************
  7. *********************************************************************
  8.  
  9.         This program is copyright (C) 1990, 1991, Regents  of  the
  10. University  of  California.   Anyone may reproduce this software,
  11. in whole or in part, provided that:
  12. (1)  Any copy  or  redistribution  must  show  the
  13.      Regents  of  the  University of California, through its
  14.      Lawrence Berkeley Laboratory, as the source,  and  must
  15.      include this notice;
  16. (2)  Any use of this software must reference this  distribu-
  17.      tion,  state that the software copyright is held by the
  18.      Regents of the University of California, and  that  the
  19.      software is used by their permission.
  20.  
  21.      It is acknowledged that the U.S. Government has  rights
  22. to this software under  Contract DE-AC03-765F00098 between the U.S.
  23. Department of Energy and the University of California.
  24.  
  25.      This software is provided as a professional  academic
  26. contribution for  joint exchange.  Thus it is experimental, is
  27. provided ``as is'', with no warranties of any kind  whatsoever,
  28. no  support,  promise  of updates, or printed documentation.
  29. Bug reports or fixes may be sent to the author, who may or may
  30. not act on them as he desires.
  31.  
  32. *********************************************************************
  33. %
  34. % usage:
  35. %    histo_simulate [-r#] [-c#] [-f#] [-S] [-E]
  36. %            [<] histogram [> [-o] graphimage]
  37. */
  38. char    usage[]="options\n\
  39. % -r -c -f    specify the simulated image window and frames.\n\
  40. %        default is 128 x 128 x input_frames.    \n\
  41. %    -S    output SHORT format.    Default is BYTE.\n\
  42. %    -E    spread fraction evenly. Otherwise rest add the padding value.\n\
  43. % [<] histogram [> graph_image]\n";
  44. /*
  45. % compile:    cc -o histo_simulate histo_simulate.c -lccs -lhipsh -lhips -lm
  46. %
  47. % AUTHOR:    Jin Guojun - LBL    2/7/91
  48. */
  49.  
  50. #include <math.h>
  51. #include "header.def"
  52. #include "imagedef.h"
  53.  
  54. U_IMAGE    uimg;
  55.  
  56. #define    buf    uimg.dest
  57. #define    pxl_bytes    uimg.pxl_out
  58.  
  59. #ifndef    HIST_TITLE_STR
  60. #define    HIST_TITLE_STR    "HIST01"
  61. #endif
  62.  
  63. #ifndef    Pad
  64. #define    Pad    0
  65. #endif
  66. #ifdef    _DEBUG_
  67. extern    int    debug;
  68. #endif
  69.  
  70. bool    Msg, Ef, Ascii;
  71.  
  72. #define    GValue()    arget(argc, argv, &i, &j)
  73.  
  74.  
  75. main(argc, argv)
  76. int    argc;
  77. char**    argv;
  78. {
  79. char    *hist_str;
  80. int    r=128, c=128, f=0, frp;
  81. MType    i, j, fsize0, fsizeNew,
  82.     numbin, binwidth, *hist;
  83. float    scale, offset=0;
  84.  
  85. format_init(&uimg, IMAGE_INIT_TYPE, HIPS, -1, *argv, "S20-1");
  86. uimg.o_form = IFMT_BYTE;
  87. uimg.pxl_out = 1;
  88.  
  89. for (i=1; i<argc; i++)
  90.     if (*argv[i] == '-')    {
  91.     j = 1;
  92.     switch(argv[i][j++])    {
  93.     case 'E':    Ef++;    break;
  94.     case 'S':    uimg.o_form = IFMT_SHORT;
  95.         uimg.pxl_out = sizeof(short);    break;
  96. #ifdef    _DEBUG_
  97.     case 'D':debug++;    break;
  98. #endif
  99.     case 'M':    Msg++;    break;
  100.     case 'O':    offset = GValue();    break;
  101.     case 'c':    c = GValue();    break;
  102.     case 'f':    f = GValue();    break;
  103.     case 'r':    r = GValue();    break;
  104.     case 'o':if (avset(argc, argv, &i, &j, 1) &&
  105.             freopen(argv[i]+j, "wb", stdout))    break;
  106.         message("output file - %s", argv[i]);
  107.     default:
  108. info:        usage_n_options(usage, i, argv[i]);
  109.     }
  110.     }
  111.     else if ((in_fp=freopen(argv[i], "rb", stdin)) == NULL)
  112.         syserr("%s - not found", argv[i]);
  113. io_test(stdin_fd, goto    info);
  114.  
  115. if ((*uimg.header_handle)(HEADER_READ, &uimg, 0, 0) || uimg.in_form != IFMT_HIST){
  116.     fseek(in_fp, 0, 0);
  117.     i = strlen(HIST_TITLE_STR);
  118.     hist_str = zalloc(1, i+1, "hstr");
  119.     fread(hist_str, 1, i, in_fp);
  120.     if (strcmp(hist_str, HIST_TITLE_STR))
  121.         syserr("input must be in histogram format");
  122.     Ascii++;
  123.     fscanf(in_fp, "%d %d %d %d %d\n", &binwidth, &numbin,
  124.         &uimg.height, &uimg.width, &uimg.frames);
  125.     uimg.o_form = IFMT_BYTE;
  126.     uimg.pxl_out = 1;
  127.     uimg.in_type = 0;
  128. }
  129. else    {
  130.     if (upread(&binwidth, 1, sizeof(binwidth), stdin) != sizeof(binwidth))
  131.         syserr("error during read bin width");
  132.     if (upread(&numbin, 1, sizeof(numbin), stdin) != sizeof(numbin))
  133.         syserr("error during read bin number");
  134. }
  135. message("%s:    image size was %d x %d\n", *argv, uimg.height, uimg.width);
  136. fsize0 = (MType)uimg.height * uimg.width;
  137. if (r<8)    r=uimg.height;
  138. if (c<8)    c=uimg.width;
  139. if (f<1)    f=uimg.frames;
  140. fsizeNew = r*c;
  141. scale = (float)fsizeNew/fsize0 + offset;
  142. uimg.height = r;    uimg.width = c;    uimg.frames = f;
  143. message("    New    image size is %d x %d x %ld bytes (scale=%.4f)\n",
  144.     r, c, pxl_bytes, scale);
  145.  
  146. (*uimg.header_handle)(HEADER_WRITE, &uimg, argc, argv, True);
  147.  
  148. hist = (int*)nzalloc(numbin, (MType)sizeof(numbin));
  149. buf = nzalloc(fsizeNew, pxl_bytes);
  150.  
  151. for (frp=0; frp<f; frp++){
  152.     if (Ascii) {
  153.         fscanf(in_fp, "%s %d", hist_str, &i);
  154.         for (j=0; j<numbin; j++){
  155.             if (j%4 == 0)    /* take away the line number */
  156.                 fscanf(in_fp, "%x ", &hist[j]);
  157.             fscanf(in_fp, "%d ", &hist[j]);
  158.         }
  159.     }
  160.     else    {
  161.         if (upread(&i, 1, sizeof(i), stdin) != sizeof(i))
  162.             syserr("can not read max count");
  163.         j = upread(hist, sizeof(*hist), numbin, stdin);
  164.         if (j != numbin)
  165.             syserr("error during read histogram %ld", j);
  166.     }
  167.     msg("maximum count in this frame is %d\n", i);
  168.     if (pxl_bytes != sizeof(short)){
  169.     register byte*    bp = (byte*)buf;
  170.         for (i=binwidth=0; i<numbin; i++){
  171.         register unsigned num = hist[i] * scale;
  172.             if (!num)    continue;
  173.             for (j=0; j<num; j++)    *bp++ = i;
  174.             binwidth += num;
  175.         if(Msg)    message("add %d pixel at gray_level %ld\n", num, i);
  176.         }
  177.         for (i=0, j=fsizeNew-binwidth; i<j; i++)
  178.             if (Ef)    *bp++=i;
  179.             else    *bp++=Pad;
  180.     }
  181.     else    {
  182.     register short*    bp = (short*)buf;
  183.         for (i=binwidth=0; i<numbin; i++){
  184.         register unsigned num = hist[i] * scale;
  185.             if (!num)    continue;
  186.             for (j=0; j<num; j++)    *bp++ = i;
  187.             binwidth += num;
  188.         if(Msg)    message("add %d pixel at gray_level %ld\n", num, i);
  189.         }
  190.         for (i=0, j=fsizeNew-binwidth; i<j; i++)
  191.             if (Ef)    *bp++=i;
  192.             else    *bp++=Pad;
  193.     }
  194.     message("%ld regular points & %ld pixel padded\n", binwidth, i);
  195.     if (fwrite(buf, pxl_bytes, fsizeNew, stdout) != fsizeNew)
  196.         syserr("w_error");
  197. }
  198. exit(0);
  199. }
  200.